﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DTBuilderLib.BinaryIO
{
    class BinaryHelpers
    {
        /// <summary>
        /// Write a lenght-prefixed string.
        /// </summary>
        /// <param name="br"></param>
        /// <param name="str"></param>
        public static void WriteString (BinaryWriter br, string str)
        {
            br.Write((int)str.Length);
            foreach (var c in str)
            {
                br.Write((byte)c);
            }
        }

        public static string ReadString(BinaryReader br)
        {
            int l = br.ReadInt32();
            byte[] bytes = br.ReadBytes(l);
            string result = "";
            foreach (var b in bytes)
            {
                result += (char)b;
            }
            return result;
        }
    }
}
